package com.shop.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shop.entity.Order;
import com.shop.entity.OrderDetail;
import com.shop.entity.Product;
import com.shop.entity.ProductCategory;
import com.shop.entity.User;
import com.shop.service.OrderDetailService;
import com.shop.service.OrderService;
import com.shop.service.ProductCategoryService;
import com.shop.service.ProductService;
import com.shop.util.ShopUtil;

public class ProductAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2799988073360312020L;

	@Autowired
	private OrderService orderService ;
	@Autowired
	private OrderDetailService orderDetailService;
	private Order order ;//订单
	private OrderDetail orderDetail ;//订单详情
	
	private Product product ;//商品
	
	private ProductCategory productCategory ;//商品分类
	private User user;
	@Autowired
	private ProductService productService ; 
	
	@Autowired
	private ProductCategoryService productCategoryService; 
	
	private static int pageIndex ;
	static int pageSum = 0;
	//根据商品分类查看商品,加分页
	public String selectProductByTypePageFirst(){
		int count = productService.count(productCategory);//记录数
		pageSum = ShopUtil.getPage(count, 3);//总页数
		System.out.println("总页数="+pageSum);
		List<Product> prolist = productService.selectProductByTypePage(productCategory, 1, 3);
		ActionContext.getContext().put("mypage", 1);
		ActionContext.getContext().getSession().put("pageSum", pageSum);
		ActionContext.getContext().put("prolist", prolist);	
		return "catlist";
	}
	
	public String selectProductByTypePage(){
		System.out.println("=="+pageSum);
		if( pageIndex < 1 ){
			pageIndex = 1;
			ActionContext.getContext().put("msg", "<script>alert('已经是第一页')</script>");			
		}
		else if( pageIndex > pageSum ){
			pageIndex = pageSum ;
			ActionContext.getContext().put("msg", "<script>alert('已经是最后一页')</script>");			
		}	
		System.out.println("//////////-------"+pageIndex);
		List<Product> prolist = productService.selectProductByTypePage(productCategory, pageIndex, 3);
		System.out.println("page2=="+prolist.size());
		ActionContext.getContext().put("prolist", prolist);
		ActionContext.getContext().put("mypage", pageIndex);
		return "catlist";
	}
	
	//根据商品分类查看商品	
	public String selectProductByType(){
		List<Product> prolist = productService.selectProductByType(productCategory);
		ActionContext.getContext().put("prolist", prolist);		
		return "catlist";
	}
	//查看所有商品
	@Deprecated
	public String findAllProduct(){
		List<Product> prolist = productService.findAllProduct();		
		ActionContext.getContext().put("prolist", prolist);
		return "catlist";
	}
	//查看商品详情
	public String selectProduct(){
		System.out.println("///////////=="+product.getEpId());
		Product productDetail = productService.selectProduct(product);
		ActionContext.getContext().put("productDetail", productDetail);
		return "infodetail";
	}
	
	
	
	/*public String addOrder(){
		Serializable seorder = orderService.addOrder(order);
		Serializable seorderDetail =orderDetailService.addOrderDetail(orderDetail);
		
		return INPUT;
	}
	*/
	static List<Product> plist = new ArrayList<Product>();
	public String joinCar(){
		System.out.println(order.getUser().getUserName());
		Serializable seorder = orderService.addOrder(order); 
		
		Product productDetail = productService.selectProduct(product);
		plist.add(productDetail);
		ActionContext.getContext().put("plist",plist);
		System.out.println("=============++"+plist.size());
		return "buy";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	public ProductService getProductService() {
		return productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	public ProductCategoryService getProductCategoryService() {
		return productCategoryService;
	}

	public void setProductCategoryService(
			ProductCategoryService productCategoryService) {
		this.productCategoryService = productCategoryService;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public OrderDetail getOrderDetail() {
		return orderDetail;
	}

	public void setOrderDetail(OrderDetail orderDetail) {
		this.orderDetail = orderDetail;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public OrderDetailService getOrderDetailService() {
		return orderDetailService;
	}

	public void setOrderDetailService(OrderDetailService orderDetailService) {
		this.orderDetailService = orderDetailService;
	}
	
	
	
	
	

}
